﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DDD从零起步.test
{
    [TestClass]
    public class XmlSerializationTest
    {
        private XmlSerialization serialization;
        [TestInitialize]
        public void InitTest()
        {
            this.serialization = new XmlSerialization(@"E:\usermodel.xml");
        }

        [TestMethod]
        public void 写一个XML文件()
        {
            User_Info user = new User_Info();
            bool flag = serialization.WriteXml<User_Info>(user);
            Assert.IsTrue(flag);
            Assert.IsFalse(serialization.WriteXml<User_Info>(null));
        }

        [TestMethod]
        public void 读一个XML文件()
        {
            User_Info user = new User_Info();
            user.UserName = "aa";
            serialization.WriteXml<User_Info>(user);
            User_Info model = serialization.ReadXml<User_Info>();
            Assert.IsNotNull(model);
            Assert.AreEqual(user.UserName, model.UserName);

            //路径不存在，应返回null
            User_Info modelnull = serialization.ReadXml<User_Info>(@"E:\notexists.xml");
            Assert.IsNull(modelnull);
        }
    }
    [Serializable]
    public class User_Info
    {
        public string UserName { get; set; }
    }
}
